/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.manager;

import br.com.deployment.entidade.selo.CorSelo;
import br.com.deployment.entidade.selo.TipoSelo;
import br.com.deployment.message.Msg;
import br.com.deployment.servico.TipoSeloServico;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.ejb.EJBAccessException;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author elyjr
 */
@ManagedBean
@SessionScoped
public class ManagerTipoSelo implements Serializable {

    @EJB
    TipoSeloServico tipoSeloServico;
    private TipoSelo tipoSelo;
    private CorSelo corSelo;

    public ManagerTipoSelo() {
    }

    @PostConstruct
    public void init() {
        tipoSelo = new TipoSelo();
        tipoSelo.setCorSelo(corSelo);
    }

    public void reinit() {
        tipoSelo = new TipoSelo();
    }

    public void clear() {
    }

    public TipoSelo getTipoSelo() {
        return tipoSelo;
    }

    public void setTipoSelo(TipoSelo tipoSelo) {
        this.tipoSelo = tipoSelo;
    }

    public void salvar() {
        try {

            tipoSeloServico.create(tipoSelo);
            Msg.messagemInfo("'Tipo Selo' Cadastrado com Sucesso !");
            reinit();
        } catch (EJBAccessException erro) {
            erro.getMessage();
        } catch (Exception erro) {
            erro.getMessage();
        }
    }

    public List<TipoSelo> completeTipoSelo(String query) {
        List<TipoSelo> suggestions = new ArrayList<TipoSelo>();
        List<TipoSelo> bd = new ArrayList<TipoSelo>();

        TipoSelo tipoSelo = new TipoSelo();
        tipoSelo.setNome(query);
        bd = tipoSeloServico.findTipoEnd(tipoSelo);

        for (TipoSelo p : bd) {
            suggestions.add(p);
        }
        return suggestions;
    }

    public void redirect(String string) {
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            tipoSelo = new TipoSelo();
            context.getExternalContext().redirect(string);
        } catch (IOException ex) {
            Logger.getLogger(ManagerTipoSelo.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
